//
// Created by bill3000 on 2/14/21.
//

#ifndef INTELLI_TIMER_HPP
#define INTELLI_TIMER_HPP

#include <semaphore.h>

#include "../mq/queue.hpp"

enum TimerMacros {
    TIMER_TO_SORTING_KERNEL = 1,
    TIMER_TO_MECH = 2,
    TIMER_INTERVAL = 10  //Interrupt evenry 10ms
};

typedef struct TimerNode {
    int to{0};   //时钟类型;
                 //1：分级分类核心逻辑线程，定时结果消息进入消息队列M2；
                 //2：机电控制逻辑线程, 定时结果消息进入消息队列M3;
    unsigned int howlong{0}; //定时时长，以毫秒ms为单位；
    int dowhat{0}; //命令，各个线程自行解释其含义
    int param{0}; //与命令相关的参数
    struct TimerNode *next{nullptr};
} TimerNode;

class OkTimer {
public:
    static void start_timer_daemon();
    static void wait_timer_daemon();
    static void stop_timer_daemon();
    static void dispose();

    static void add_timer(TimerNode &tn);

private:
    static TimerNode *timers;
    static sem_t sem;
    static Queue *mq;

    static void timer_daemon();
    static void check_timer(int);
    static void handle_time_out(TimerNode &t);
};

#endif //INTELLI_TIMER_HPP
